package activity

import (
	"fmt"
	"errors"
	"github.com/jmoiron/sqlx"
	"gitee.com/zhanglibin/spike/lib/db"
	"gitee.com/zhanglibin/spike/conf"
)

var (
	dbClient *sqlx.DB
)

type Activity struct {
	Id int `json:"id"`
	Activity_name string `json:"activity_name"`
}

func _init() {
	dbinfo := conf.C.Mysql["server1"]
	config := db.DbInfo{
		Engine: dbinfo.Engine,
		User: dbinfo.User,
		Password: dbinfo.Password,
		Host: dbinfo.Host,
		Port: dbinfo.Port,
		Dbname: dbinfo.Dbname,	
	} 
	dbClient = db.ConnectMysql(config)
}

// 根据id查询活动详情
func GetActivityInfoById(id int) (result Activity, err error){
	_init()
	err = dbClient.Get(&result, "select * from activity where id = ?", id)
	if err != nil {
		fmt.Println("mysql exec error: " + err.Error())
		return result, errors.New(fmt.Sprintf("mysql exec error: %v",err.Error()))
	}
    return result, nil
}

// // 新增数据
// func InsertUserInfo(username string, age int, createtime string, password string, phone string)(errorMsg string) {
// 	DB := db.ConnectMysql()
// 	_, err := DB.Exec("insert into users (username, age, createtime, password, phone ) values (?, ?, ?, ?, ?)", username, age, createtime, password, phone)
// 	if err != nil {
// 		errorMsg = "mysql exec error:" + err.Error()
// 	}
// 	return errorMsg
// }

// // 列表
// func GetListByCondition(username string, phone string, page int) (result []User, errorMsg string) {
// 	DB := db.ConnectMysql()
// 	// var result []User
// 	err:=DB.Select(&result, "select * from users where username = ? and phone = ? limit ?,10", username, phone, page)
// 	if err != nil {
// 		errorMsg = "mysql exec error:" + err.Error()
// 	}
// 	return result,errorMsg
// }